
package com.probiz.estore.customer.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.common.model.customer.Customer;
import com.probiz.estore.common.model.customer.Wishlist;
import com.probiz.estore.common.model.customer.WishlistItem;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.customer.dao.WishlistDao;
import com.probiz.estore.customer.service.WishlistManager;
import com.probiz.estore.product.service.ProductManager;
import com.probiz.estore.webapp.util.RequestContext;

/**
 * Wishlist Business Delegate (Proxy) implementation to handle communication
 * between web and persistence layer. Implementation of WishlistManager
 * interface. Developer introduced interfaces should be declared here. Won't get
 * overwritten.
 */
public class WishlistManagerImpl extends GenericManagerImpl<Wishlist> implements WishlistManager {

	private ProductManager	productManager	= null;
	private WishlistDao wishlistDao			= null;

	public void setProductManager(ProductManager in_productManager) {
		this.productManager = in_productManager;
	}

	@Override
	protected void initManager() {
		this.dao = wishlistDao;
	}

	@Override
	protected void preDelete(Wishlist entity) {
		
	}

	@Override
	protected void preSave(Wishlist entity) {
		
	}

	/**
	 * @see com.probiz.estore.customer.service.WishlistManager#getAllByCustomerId(Integer)
	 */
	public List<Wishlist> getAllByCustomerId(Integer customerId) {
		return wishlistDao.getByCustomerId(customerId);
	}

	/**
	 * @see com.probiz.estore.customer.service.WishlistManager#AddWishListItem(String)
	 */
	public void addWishListItem(String productCode) {

		Wishlist wishlist = wishlistDao.getCustomerDefaultWishlist(RequestContext
				.getCurrentUserId());

		if (wishlist != null) {
			WishlistItem item = new WishlistItem();
			Product product = productManager
					.getProductByProductCode(productCode);
			item.setProduct(product);
			item.setPurchasedQty(Integer.valueOf(0));
			item.setQuantity(Integer.valueOf(1));
			item.setWishlist(wishlist);
			item.setCreateTime(new Date());
			wishlist.getWishlistItems().add(item);
		}
		// update to database
		wishlistDao.save(wishlist);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.WishlistManager#addSaveWishListItem(java.lang.String, java.lang.String, java.lang.String)
	 */
	public String addSaveWishListItem(String wishlistId,
			String newWishlistName, String productCode) {

		if (StringUtils.isEmpty(productCode)) {
			return "fail";
		}

		Product product = productManager.getProductByProductCode(productCode);

		Wishlist wishlist = null;

		if (!wishlistId.equals("0")) {// this wish list has exist in database
			wishlist = wishlistDao.getById(new Integer(wishlistId));
		} else {// create new wish list
			wishlist = new Wishlist();
			wishlist.setWishListTitle(newWishlistName);

			Customer customer = (Customer) RequestContext.getCurrentUser();
			if (customer == null) {
				return "unlogin";
			}
			// log.debug("customer name:" + customer.getUsername());
			wishlist.setRecipientEmailList(customer.getEmail());
			wishlist.setCustomer(customer);
			wishlist.setCreateTime(new Date());
			wishlist.setShareLevel(Wishlist.SHARED_LEVEL_ANY);
			wishlist.setWishListType(Wishlist.WISH_LIST_TYPE_NORMAL);
			wishlist.setAddress(null);
		}

		// check this product has exist in this wish list
		boolean isExist = wishlistDao.isProductExistInWishList(new Integer(wishlistId),
				product.getProductId());
		if (!isExist) {
			WishlistItem wishlistItem = new WishlistItem();
			wishlistItem.setCreateTime(new Date());
			wishlistItem.setQuantity(Integer.valueOf(1));
			wishlistItem.setPurchasedQty(Integer.valueOf(0));
			wishlistItem.setWishlist(wishlist);
			wishlistItem.setProduct(product);
			wishlist.getWishlistItems().add(wishlistItem);
		} else {
			return "exist";
		}
		// add wishlist item and
		// (add/update) wish list
		wishlistDao.save(wishlist);
		return "success";
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.WishlistManager#addDefaultWishlist(com.probiz.estore.common.model.customer.Customer)
	 */
	public void addDefaultWishlist(Customer customer) {
		// add a default wishlist for this customer.
		Wishlist wishlist = new Wishlist();
		wishlist.setCustomer(customer);
		wishlist.setIsDefault(Wishlist.DEFAULT_LIST);
		wishlist.setShareLevel(Wishlist.SHARED_LEVEL_ANY);
		wishlist.setWishListTitle(Wishlist.WISHLIST_DEFAULT_TITLE);
		wishlist.setWishListType(Wishlist.WISH_LIST_TYPE_NORMAL);
		wishlist.setCreateTime(new Date());
		wishlistDao.save(wishlist);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.WishlistManager#getCustomerDefaultWishlist(java.lang.Integer)
	 */
	public Wishlist getCustomerDefaultWishlist(Integer customerId) {
		return wishlistDao.getCustomerDefaultWishlist(customerId);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.WishlistManager#updateDefaultWishlist(java.lang.Integer, java.lang.Integer)
	 */
	public void updateDefaultWishlist(Integer wishlistId, Integer customerId) {
		wishlistDao.updateDefaultWishList(wishlistId, customerId);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.WishlistManager#deleteWishlist(java.lang.Integer)
	 */
	public void deleteWishlist(Integer wishlistId) {
		wishlistDao.deleteWishlist(wishlistId);
	}

	public void setWishlistDao(WishlistDao wishlistDao) {
		this.wishlistDao = wishlistDao;
	}

}
